package com.xkdx.guangguang.util;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.os.Environment;

public class SaveImageToSdka {

	String rootPath = Environment.getExternalStorageDirectory() + "/GuangGuang/photo/cachebrand/";
	String sharePath = Environment.getExternalStorageDirectory() + "/GuangGuang/share/";
	String homeInfoPath = Environment.getExternalStorageDirectory() + "/GuangGuang/homeInfo/";
	String headPath = Environment.getExternalStorageDirectory() + "/GuangGuang/user/";
	String fileName = null;
	String url = null;

	public SaveImageToSdka() {
		super();
	}

	public byte[] getImage() throws Exception {
		URL url1 = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
		conn.setConnectTimeout(5 * 1000);
		conn.setRequestMethod("GET");
		InputStream inStream = conn.getInputStream();
		if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			return readStream(inStream);
		}
		return null;
	}

	public InputStream getImageStream() throws Exception {
		URL url1 = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
		conn.setConnectTimeout(5 * 1000);
		conn.setRequestMethod("GET");
		InputStream inStream = conn.getInputStream();
		if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			return inStream;
		}
		return null;
	}

	public static byte[] readStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		outStream.close();
		inStream.close();
		return outStream.toByteArray();
	}

	public void saveFile(Bitmap bm, String fileName) throws IOException {
		File dirFile = new File(rootPath);
		if (!dirFile.exists()) {
			dirFile.mkdirs();
		}
		File myCaptureFile = new File(rootPath + fileName);
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
		bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
		bos.flush();
		bos.close();
	}

	/**
	 * author zhangshun add 用于分享新品时的缓存sdcard
	 * 
	 * @param bm
	 * @param fileName
	 * @throws IOException
	 */
	public void saveShareFile(Bitmap bm, String fileName) throws IOException {
		File dirFile = new File(sharePath);
		if (!dirFile.exists()) {
			dirFile.mkdirs();
		}
		File myCaptureFile = new File(sharePath + fileName);
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
		bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
		bos.flush();
		bos.close();

	}

	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @param bm
	 * @param fileName
	 * @throws IOException
	 */
	public void saveHomeInfoFile(Bitmap bm, String fileName) throws IOException {
		BufferedOutputStream bos = null;
		try {
			File dirFile = new File(homeInfoPath);
			if (!dirFile.exists()) {
				dirFile.mkdirs();
			}
			File myCaptureFile = new File(homeInfoPath + fileName);
			// Log.i("path",myCaptureFile.getAbsolutePath());
			bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
			bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
			bos.flush();
			bos.close();
			bos = null;
		} catch (Exception e) {

		} finally {
			if (bos != null) {
				bos.flush();
				bos.close();
			}
		}
	}

	/**
	 * @param bm
	 * @param fileName
	 * @throws IOException
	 */
	public void saveHeadInfoFile(Bitmap bm, String fileName) throws IOException {
		BufferedOutputStream bos = null;
		try {

			File dirFile = new File(headPath);
			if (!dirFile.exists()) {
				dirFile.mkdirs();
			}
			File myCaptureFile = new File(headPath + fileName);
			// Log.i("path",myCaptureFile.getAbsolutePath());
			bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
			bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
			bos.flush();
			bos.close();
			bos = null;
		} catch (Exception e) {
			// try {
			// bos.flush();
			// bos.close();
			// } catch (IOException e1) {
			// e1.printStackTrace();
			// }

		} finally {

			if (bos != null) {
				bos.flush();
				bos.close();
			}

		}
	}

}
